﻿(function($) {
    $.fn.tips = function(options) {

        options = $.extend({}, $.fn.tips.defaults, options);
        
        return this.each(function() {
            
            var curObj = this;
            var opts = $.fn.tips.elementOptions(curObj, options);
            var isTip = false;	//是否已经提示
            
            $(curObj).hover(function(){
            	//如果当前鼠标位置对象不是 焦点元素 ，则提示
            	if ( this != document.activeElement ){
            		$.fn.tips.mover(this,opts);
            		isTip = true;
            	}
            }, function() {
            	if( this != document.activeElement ){
            		$.fn.tips.mout(this,opts);
            		isTip = false;
            	}
            	
            });
            
            $(curObj).focus(function(evt){
            	if( !isTip ){
            		$.fn.tips.mover(this,opts);
            		isTip = true;
            	}
            });
            $(curObj).blur(function(evt){
            	$.fn.tips.mout(this,opts);
            	isTip = false;
            });
            
        });
        
    };
    
    $.fn.tips.elementOptions = function(ele, options) {
        return $.metadata ? $.extend({}, options, $(ele).metadata()) : options;
    };
    
    $.fn.tips.defaults = {
        fade: false,
        fallback: '',
        gravity: 'n',
        html: false,
        title: 'title'
    };
    
    $.fn.tips.mover = function(ele, opts) {
    	
         $.data(ele, 'cancel.tips', true);

         var tip = $.data(ele, 'active.tips');
         if (!tip) {
             tip = $('<div class="tips"><div class="tips-inner"/></div>');
             tip.css({position: 'absolute', zIndex: 100000});
             $.data(ele, 'active.tips', tip);
         }

         if ($(ele).attr('title') || typeof($(ele).attr('original-title')) != 'string') {
             $(ele).attr('original-title', $(ele).attr('title') || '').removeAttr('title');
         }

         var title;
         if (typeof opts.title == 'string') {
             title = $(ele).attr(opts.title == 'title' ? 'original-title' : opts.title);
         } else if (typeof opts.title == 'function') {
             title = opts.title.call(ele);
         }

         tip.find('.tips-inner')[opts.html ? 'html' : 'text'](title || opts.fallback);

         var pos = $.extend({}, $(ele).offset(), {width: ele.offsetWidth, height: ele.offsetHeight});
         tip.get(0).className = 'tips'; // reset classname in case of dynamic gravity
         tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
         var actualWidth = tip[0].offsetWidth, actualHeight = tip[0].offsetHeight;
         var gravity = (typeof opts.gravity == 'function') ? opts.gravity.call(ele) : opts.gravity;

         switch (gravity.charAt(0)) {
             case 'n':
                 tip.css({top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tips-north');
                 break;
             case 's':
                 tip.css({top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tips-south');
                 break;
             case 'e':
                 tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}).addClass('tips-east');
                 break;
             case 'w':
                 tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}).addClass('tips-west');
                 break;
         }

         if (opts.fade) {
             tip.css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: 0.9});
         } else {
             tip.css({visibility: 'visible',opacity: 0.8});
         }

     };
     
     $.fn.tips.mout = function( ele, opts){
     	$.data(this, 'cancel.tips', false);
        var self = ele;
        setTimeout(function() {
            if ($.data(this, 'cancel.tips')) return;
            var tip = $.data(self, 'active.tips');
            if (opts.fade) {
                tip.stop().fadeOut(function() { $(this).remove(); });
            } else {
                tip.remove();
            }
        }, 100);
     };
    
    /*
    $.fn.tips.autoNS = function() {
        return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
    };
    
    $.fn.tips.autoWE = function() {
        return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
    };
    */
    
})(jQuery);
